<template>
  <!-- 采集模板设定 -->
  <div class="app-container">
    <el-row>
      <el-col :span="24">
        <div class="form-container">
          <el-form :inline="true" label-width="auto" :model="filters" class="selectFrom">
            <el-form-item label="字段名称" prop="fieldName">
              <el-input v-model="filters.fieldName" size="small" placeholder="请输入字段名称" clearable />
            </el-form-item>
          </el-form>
        </div>
        <div class="btns-group">
          <superviseBtns :single="single" has-permi="descriptorPersonal" />
        </div>
      </el-col>
      <el-col :span="24">
        <!-- 表格展示start -->
        <el-table
          ref="multipleTable"
          v-loading="tableLoading"
          stripe
          class="table-list"
          :class="highLevelSet===true ? (needTagsView===true ? 'el-body-page-roll-tag-height' : 'el-body-page-roll-height') :
            (needTagsView===true ? 'el-body-page-roll-tag' : 'el-body-page-roll')"
          :data="tableData"
          :row-style="rowClass"
          row-key="id"
          :default-expand-all="true"
          :tree-props="{children: 'children', hasChildren: 'hasChildren'}"
          @selection-change="handleSelectionChange"
          @row-click="rowChecked"
        >
          <el-table-column type="selection" width="55" align="center" />
          <el-table-column prop="fieldName" label="字段名称" min-width="180" header-align="center" align="left" />
          <el-table-column prop="orderNum" label="排序" min-width="120" align="center" />
          <el-table-column prop="isRequired" label="必填" min-width="120" align="center">
            <template slot-scope="{row}">
              <span v-if="row.isRequired === '0'">是</span>
              <span v-if="row.isRequired === '1'">否</span>
            </template>
          </el-table-column>
          <el-table-column prop="status" label="状态" min-width="120" align="center">
            <template slot-scope="{row}">
              <span v-if="row.status === '0'">是</span>
              <span v-if="row.status === '1'">否</span>
            </template>
          </el-table-column>
          <!-- <el-table-column prop="createTime" label="创建时间" width="180" align="center" /> -->
        </el-table>
        <!-- 表格展示end-->
        <!-- 分页-->
        <pagination
          :total="total"
          :page.sync="filters.pageNum"
          :limit.sync="filters.pageSize"
          @pagination="getList"
        />
      </el-col>
    </el-row>
    <!-- 字段新增弹框 -->
    <addDialog ref="addDialog" />
    <!-- 字段详情弹框 -->
    <detailDialog ref="detailDialog" />
  </div>
</template>

<script>
// 按钮组件
import superviseBtns from '@/components/Common/Btns/superviseBtns'
// import { listSupperType, removeSupperType } from '@/api/superviseBaseInfo/typeManage/organizationType'
import addDialog from './components/addDialog'
import detailDialog from './components/detailDialog'
import { mapState } from 'vuex'
export default {
  name: 'PersonneType',
  components: {
    detailDialog,
    addDialog,
    superviseBtns
  },
  data() {
    return {
      tableLoading: false, // 表格加载
      highLevelSet: false, // 高级搜索
      // 筛选条件
      filters: {
        pageNum: 1,
        pageSize: 20,
        fieldName: ''
      },
      /* 列表 start */
      // 分页总条数
      total: 0,
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 选中表格对话框的存储的值
      multipleSelection: [],
      // 高亮样式
      selectRow: [],
      // 表格数据
      tableData: [
        {
          id: '1',
          fieldName: '描述深度(m)',
          fieldVal: 'depth',
          orderNum: 1,
          isRequired: '0',
          status: '1',
          isExrernal: '1',
          createTime: '2021-05-05',
          children: [
            {
              id: '1001',
              fieldName: '由',
              parentId: '1',
              fieldVal: 'startDepth',
              orderNum: 1,
              isRequired: '0',
              status: '1',
              isExrernal: '1',
              children: [],
              createTime: '2021-05-05'
            },
            {
              id: '1002',
              fieldName: '至',
              fieldVal: 'endDepth',
              orderNum: 2,
              parentId: '1',
              isRequired: '0',
              status: '1',
              isExrernal: '1',
              children: [],
              createTime: '2021-05-05'
            }
          ]
        },
        {
          id: '2',
          fieldName: '地层描述',
          fieldVal: 'depth',
          orderNum: 2,
          isRequired: '0',
          status: '1',
          isExrernal: '1',
          createTime: '2021-05-05',
          children: [
            {
              id: '2001',
              fieldName: '土的名称',
              fieldVal: 'soilName',
              orderNum: 1,
              isRequired: '0',
              status: '1',
              parentId: '2',
              isExrernal: '1',
              createTime: '2021-05-05',
              children: []
            },
            {
              id: '2002',
              fieldName: '颜色',
              fieldVal: 'soilColor',
              orderNum: 2,
              parentId: '2',
              isRequired: '0',
              status: '1',
              isExrernal: '1',
              createTime: '2021-05-05',
              children: []
            },
            {
              id: '2003',
              fieldName: '其他性质',
              fieldVal: 'ohterProperties',
              orderNum: 3,
              isRequired: '0',
              parentId: '2',
              status: '1',
              isExrernal: '1',
              createTime: '2021-05-05',
              children: []
            },
            {
              id: '2004',
              parentId: '2',
              fieldName: '光泽反应',
              fieldVal: 'glossReaction',
              orderNum: 4,
              isRequired: '0',
              status: '1',
              isExrernal: '1',
              children: [],
              createTime: '2021-05-05'
            }
          ]
        },
        {
          id: '3',
          fieldName: '密度',
          fieldVal: 'depth',
          orderNum: 3,
          isRequired: '0',
          status: '1',
          isExrernal: '1',
          createTime: '2021-05-05',
          children: []
        }
      ]
      /* 列表 end */
    }
  },
  computed: {
    ...mapState({
      needTagsView: (state) => state.settings.tagsView
    }),
    // 高级搜索显示隐藏
    handleTagShow() {
      return this.highLevelSet
    }
  },
  mounted() {
    // 获取列表数据
    this.getList()
  },
  methods: {
    // 获取列表数据
    async getList() {
      this.tableLoading = true
      // const res = await listSupperType(this.filters)
      // console.log(res)
      this.tableLoading = false
      // this.addTaleData()
    },
    // list表单补空白行
    addTaleData() {
      if (this.tableData.length < this.filters.pageSize) {
        var arr = {}
        this.tableData.push(arr)
        this.addTaleData()
      }
    },
    /* 表单行选中事件 start */
    // 单击每行时每行的复选框选中
    rowChecked(row) {
      // 将选中的数组赋给常量 selectData
      const selectData = this.multipleSelection
      // 调用表格的方法将所有已选的都取消
      this.$refs.multipleTable.clearSelection()
      // 判断选中的数组个数是不是等于 1
      if (selectData.length === 1) {
        // 循环判断有没有和当前行的相同的
        selectData.forEach((item) => {
          // 判断 如果当前的一行被勾选, 再次点击的时候就会取消选中
          if (item === row) {
            this.$refs.multipleTable.toggleRowSelection(row, false)
          } else {
            this.$refs.multipleTable.toggleRowSelection(row, true)
          }
        })
      } else {
        // 不等于1时
        this.$refs.multipleTable.toggleRowSelection(row, true)
      }
    },
    // 高亮样式
    rowClass({ row, rowIndex }) {
      if (this.selectRow.includes(rowIndex)) {
        return { 'background-color': 'rgba(185,221,249,.75)' }
      }
    },
    // 点击表格前面选框时触发的事件
    handleSelectionChange(selection) {
      if (JSON.stringify(selection[0]) === '{}') {
        this.$refs.multipleTable.clearSelection()
      } else {
        this.multipleSelection = selection

        this.single = selection.length !== 1
        this.multiple = !selection.length
        this.selectRow = []
        if (selection.length > 0) {
          selection.forEach((item, index) => {
            this.selectRow.push(this.tableData.indexOf(item))
          })
        }
        // 如果选择的数组大于1
        if (selection.length > 1) {
          // 清空所有的选中
          this.$refs.multipleTable.clearSelection()
          // 将数组的最后一个选中
          this.$refs.multipleTable.toggleRowSelection(selection.pop())
        }
      }
    },
    /* 表单行选中事件 end */

    /* 按钮 start */
    // 查询
    handleQuery() {
      this.getList()
    },
    // 重置搜索条件
    resetQuery() {
      this.filters = {
        pageNum: 1,
        pageSize: 20,
        fieldName: ''
      }
      this.getList()
    },
    // 新增
    handleAdd() {
      this.$refs.addDialog.handleAdd('新增字段')
    },
    // 编辑
    handleUpdate() {
      this.$refs.addDialog.handleAdd('修改字段', this.multipleSelection[0])
    },
    // 详情
    handleDetail() {
      this.$refs.detailDialog.handleAdd('字段详情', this.multipleSelection[0])
    },
    // 修改
    handleDelete() {
      const child = this.multipleSelection
      this.$confirm('此操作将永久删除该字段, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          this.tableLoading = true
          var ids = ''
          for (var i = 0; i < child.length; i++) {
            ids += child[i].id + ','
          }
          // 去掉最后一个逗号
          if (ids.length > 0) {
            ids = ids.substr(0, ids.length - 1)
          }
          // removeSupperType(ids).then(res => {
          this.tableLoading = false
          this.msgSuccess('删除成功！')
          // 重新加载表格数据
          this.getList()
          // })
        })
        .catch(() => {})
    }
    /* 按钮 end */
  }
}
</script>

